﻿using System.Collections;
using UnityEngine;

//用于实现tilemap破碎效果
public class Fragment :MyEffect
{
    private SpriteRenderer m_spriteRenderer;
    private float timer;
    private Color originColor;
    private Color targetColor;

    private bool b_started;
    private Vector3 drct_move;
    private float speed;

    public override void OnCreate()
    {
        base.OnCreate();
        originColor = Color.white;
        targetColor = originColor.ResetAlpha(0f);
        m_spriteRenderer = GetComponent<SpriteRenderer>();
    }

    /// <param name="drct">飞出方向</param>
    /// <param name="s">初始速度</param>
    /// <param name="t">存在时间</param>
    public void Move(Vector2 drct,float s,float t)
    {
        drct_move = drct;
        speed = s;
        t_exist = t;
        b_started = true;
    }

    protected override void OnActivate()
    {
        base.OnActivate();
    }

    protected override IEnumerator LifeCycle()
    {
        for(; !b_started; )
        {
            yield return null;
        }
        for (timer = 0; timer < t_exist; timer += Time.fixedDeltaTime)
        {
            transform.position += speed * Time.fixedDeltaTime * drct_move;
            m_spriteRenderer.color = Color.Lerp(originColor, targetColor, timer / t_exist);
            yield return new WaitForFixedUpdate();
        }
        Recycle();
    }
}
